xquery version "3.0";

module namespace home = "pages/home";

declare variable $home:paste-users := "/db/tk_data/users/users.xml";
declare variable $home:login-form := <form method="POST">
                    <label for="user">Пользователь:</label><input name="user" type="text"/>
                    <label for="password">Пароль:</label><input name="password" type="password"/>
                    <input type="submit" value="Войти"/>
                </form>;

declare function home:page($message){
    util:declare-option("exist:serialize", "method=html5 media-type=text/html indent=no"),
    <html>
        <head>
            <link rel="stylesheet" type="text/css" href="bootstrap.min.css"/>
            <script src="jquery.min.js" type="text/javascript"></script>
            <script src="script.js" type="text/javascript"></script>
        </head>
        <body>
            <h1>Здравствуйте этот сайт был создан на основе exist-db</h1>
            <h2><a href="/feed">Обратная связь</a></h2>
            {$message}
            <object width="640" height="480"><param name="movie" value="http://www.youtube.com/v/bXpUvxOdQxE?version=3&amp;hl=ru_RU"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/bXpUvxOdQxE?version=3&amp;hl=ru_RU" type="application/x-shockwave-flash" width="640" height="480" allowscriptaccess="always" allowfullscreen="true"></embed></object>
        </body>
    </html>
};

declare function home:create-user($user, $pass){
    let $login := xmldb:login("/db/", "vitalik", "12")
    let $record := <user name="{$user}" pass="{$pass}" id="{util:uuid()}"/>
    return update insert $record into doc($home:paste-users)/users
};

declare function home:html(){
    if (request:get-method() = 'POST') then (
        let $user := request:get-parameter("user", "")
        let $pass := request:get-parameter("password", "")
        return if (string-length($user) = 0 or string-length($pass) = 0 and not(session:exists())) then (
            (:здесь возврат страницы с сообщением о пустых логине и пароле:)
            (home:page(($home:login-form, <div>Вы не ввели логин или пароль {empty($user)}, {empty($pass)}, {empty($user) or empty($pass)}</div>)))
        )
        else (
            (:let $user-id := request:get-cookie-value("user"):)
            let $hello := home:page(<div>Привет {$user}</div>)
            let $check-user := (
                let $search := doc($home:paste-users)//user[@user/string() = $user]
                return if (empty($search)) then true() else false()
            )
            let $login := if ($check-user) then home:create-user($user, $pass)
            else false()
            return (
                session:create(),
                (:(
                    let $uid := doc($home:paste-users)//user[@user/string() = $user]/@id/string()
                    return (
                        session:set-attribute("user", $uid),
                        response:set-cookie("user", $uid)
                    )
                ),:)
                
                $hello
            )
        )
    )
    else (
        util:declare-option("exist:serialize", "method=html5 media-type=text/html indent=no"),
        home:page($home:login-form)
    )
};